app.controller('userViewController', ['$scope', 'httpWrapService','validationService', '$state', '$stateParams','dialogs',
                        'AlertFactory','GridFactory','CropImgFactory',
                    function ($scope, httpWrapService, validationService,  $state, $stateParams, dialogs, AlertFactory,GridFactory, CropImgFactory) {
    $scope.entity = {};
    $scope.group = {};

    $scope.id = $stateParams.id;
    $scope.type = $stateParams.type || 'add';
    $scope.isAdmin = $stateParams.isAdmin;
    $scope.Alert = AlertFactory.newInstance();

    $scope.setImg = function(img){
        $scope.entity.picture = img;
    }
    $scope.CropImg = CropImgFactory.newInstance($scope.setImg);

    var loadUser = function(){
        if($scope.id) {
            httpWrapService.post('system/user/view/' + $scope.id + '.do').then(function (res) {
                $scope.entity = res.data.entity;
                $scope.type = 'update';
                $scope.confirmPassword = $scope.entity.password;

                // 初始化选择权限
                if(res.data.map.roles) {
                    var roles  = {};
                    for(var i = 0; i < res.data.map.roles.length; i++) {
                        var ra = res.data.map.roles[i];
                        roles[ra.roleId] = ra;
                    }

                    // 选中行
                    $scope.roleGrid.selectRows(roles);
                }
                // 初始化选择权限
                if(res.data.map.groups && res.data.map.groups.length > 0) {
                    $scope.group = {
                        id: res.data.map.groups[0].groupId
                    };
                }
            });
        }
    };


    $scope.roleGrid = GridFactory.newInstance({
        url : "system/role/list.do",
        loadSuccessCallbak : loadUser,
        columnDefs : [
            {name: "角色名", field: "name"},
            {name: "角色", field: "role"}
        ],
        gridOptions : {
            multiSelect : true
        }
    });
    $scope.roleGrid.load();

    $scope.unique = function(value){
        return validationService.unique({
            entityName : "common.rbac.po.User",
            field : "name",
            value : value,
            id : $scope.id
        })
    }

    $scope.save = function () {
        var rows = $scope.roleGrid.getSelectedRows();
        var roles = [];
        if(rows) {
            for(var i = 0; i < rows.length; i++) {
                var obj = {
                    roleId : rows[i].id,
                    userId : $scope.id
                }
                roles.push(obj);
            }
        }

        var groups = [{
            groupId : $scope.group.id,
            userId : $scope.id
        }];

        httpWrapService.defaultSave( 'system/user/' + $scope.type + '/' + $scope.isAdmin + '.do',{
            entity : $scope.entity,
            groups : groups,
            roles : roles
        }, $scope).then(function (res) {
            $state.go('app.system.user.list');
        });

        // 保存成功返回列表页面

    }
    $scope.canle = function () {
        // 后退到列表页面
        $state.go('app.system.user.list');
    }

    $scope.openGroupTree = function () {
        dialogs.create('system-tpl\/group\/tree.html','sysUserChangeGroupTreeCtrl',$scope.group,{size:'md',keyboard: true,backdrop: true,windowClass: 'my-class',copy : false});
    }

}]);


app.controller('sysUserChangeGroupTreeCtrl',['$scope', '$uibModalInstance','data','httpWrapService','dialogs', function($scope,$uibModalInstance,data, httpWrapService,dialogs){
    $scope.data = data;
    $scope.treeData = [];


    $scope.itemClicked = function(item){
        if($scope.data.id != item.$item.id) {
            if($.inArray($scope.data.id, item.$item.parentIds) > 0) {
            } else {
                angular.copy(item.$item, $scope.data);
            }
        }
        $uibModalInstance.close();
    }

    $scope.cancel = function(){
        $uibModalInstance.close('Canceled');
    };

    httpWrapService.post("system/group/tree.do").then(function(res){
        $scope.treeData = res.data.data;
    });

}]);